package com.agileai.bpm.controller.define;

import java.util.ArrayList;
import java.util.List;

import com.agileai.bpm.common.ProcessDefineUtil;
import com.agileai.bpm.controller.ui.VariableBox;
import com.agileai.bpm.model.BizAttributeType;
import com.agileai.bpm.model.InterceptorModelDocument.InterceptorModel;
import com.agileai.bpm.model.ProcessDefineDocument.ProcessDefine;
import com.agileai.bpm.model.WatchInvokerType;
import com.agileai.common.KeyGenerator;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.domain.FormSelect;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.DispatchRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.StringUtil;

public class WfProcessInvokerEditHandler extends WfProcessInfoHandler {
	
	public ViewRenderer prepareDisplay(DataParam param){
		this.mergeParam(param);
		this.setAttributes(param);
		this.initAttriubtes(param);
		return new LocalRenderer(getPage());
	}
	
	public ViewRenderer doSaveAction(DataParam param){
		String rspText = null;
		try {
			String activityId = param.get("WFA_ID");
			String interceptorId = param.get("interceptorId");
			String positionType = param.get("positionType");
			ProcessDefine processDefine = getProcessDefine();
			InterceptorModel interceptorModel = null;
			if (StringUtil.isNullOrEmpty(interceptorId)){
				interceptorModel = ProcessDefineUtil.createInterceptorModel(processDefine, positionType, activityId);
				String id = KeyGenerator.instance().genKey();
				interceptorModel.setId(id);
			}
			else{
				interceptorModel = ProcessDefineUtil.getInterceptorModel(processDefine, positionType, activityId, interceptorId);
			}
			this.buildInterceptor(interceptorModel, param);
			rspText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(rspText);
	}
	
	@PageAction
	public ViewRenderer selectProcessCode(DataParam param){
		storeParam(param);
		return new DispatchRenderer(getHandlerURL(WfProcessCodeSelectHandler.class)+"&fromHandlerId=WfProcessInvokerEdit");
	}
	
	@PageAction
	public ViewRenderer selectProcessVar(DataParam param){
		storeParam(param);
		return new DispatchRenderer(getHandlerURL(WfVariableCodeSelectHandler.class)+"&fromHandlerId=WfProcessInvokerEdit");
	}	
	
	@PageAction
	public ViewRenderer selectActivityCode(DataParam param){
		storeParam(param);
		return new DispatchRenderer(getHandlerURL(WfWatchActivitySelectHandler.class));
	}	
	
	private void buildInterceptor(InterceptorModel interceptorModel,DataParam param){
		WatchInvokerType watchInvokerType = interceptorModel.getWatchInvoker();
		if  (watchInvokerType == null){
			watchInvokerType = interceptorModel.addNewWatchInvoker();
		}
		String interceptorName = param.get("interceptorName");
//		String variableCode = param.get("variableCode");
//		String isSynchronous = param.get("isSynchronous");
		
		interceptorModel.setName(interceptorName);
//		interceptorModel.setVariableCode(variableCode);
//		interceptorModel.setSynchronous(Boolean.parseBoolean(isSynchronous));
		
		watchInvokerType.setBizRecordId(param.get("bizRecordId"));
		watchInvokerType.setProcessCode(param.get("processCode"));
		watchInvokerType.setActivityCode(param.get("activityCode"));
		
		int attributeSize = param.getInt("attributeSize");
		if (attributeSize > 0){
			BizAttributeType[] attributeTypes = new BizAttributeType[attributeSize];
			for (int i=0;i < attributeSize;i++){
				BizAttributeType bizAttributeType = BizAttributeType.Factory.newInstance();
				String code = param.get("attrCode_"+i);
				String value = param.get("attrValue_"+i);
				bizAttributeType.setCode(code);
				bizAttributeType.setValue(value);
				attributeTypes[i] = bizAttributeType;
			}
			watchInvokerType.setBizAttributeArray(attributeTypes);
		}else{
			watchInvokerType.setBizAttributeArray(new BizAttributeType[0]);
		}
	}
	
	public ViewRenderer doBackAction(DataParam param){
		this.storeParam(param);
		return new DispatchRenderer(getHandlerURL(WfInterceptorTypeSelectHandler.class));
	}

	private void initAttriubtes(DataParam param){
		FormSelect formSelect = FormSelectFactory.create("InterceptorType");
		formSelect.setHasBlankValue(false);
		String interceptorType = param.get("interceptorType");
		formSelect.setSelectedValue(interceptorType);
		this.setAttribute("interceptorTypeSelect", formSelect);
		
		String activityId = param.get("WFA_ID");
		String interceptorId = param.get("interceptorId");
		if (StringUtil.isNullOrEmpty(interceptorId)){
			interceptorId = (String)this.getSessionAttribute(WfVariableCodeSelectHandler.TEMP_INTERCEPTORID);
			if (interceptorId != null){
				this.removeSessionAttribute(WfVariableCodeSelectHandler.TEMP_INTERCEPTORID);
				this.setAttribute("interceptorId", interceptorId);
			}
		}		
		String positionType = param.get("positionType");
		List<DataRow> attributes = new ArrayList<DataRow>();
		this.setRsList(attributes);
		if (StringUtil.isNotNullNotEmpty(interceptorId)){
			ProcessDefine processDefine = getProcessDefine();
			InterceptorModel interceptorModel = ProcessDefineUtil.getInterceptorModel(processDefine
					,positionType, activityId, interceptorId);
			
			String interceptorName = param.get("interceptorName",interceptorModel.getName());
			WatchInvokerType watchInvokerType = interceptorModel.getWatchInvoker();
			String bizRecordId = param.get("bizRecordId",watchInvokerType.getBizRecordId());
			String processCode = param.get("processCode",watchInvokerType.getProcessCode());
			String activityCode = param.get("activityCode",watchInvokerType.getActivityCode());
			
			this.setAttribute("bizRecordId", bizRecordId);
			this.setAttribute("processCode", processCode);
			this.setAttribute("activityCode", activityCode);
			this.setAttribute("interceptorName", interceptorName);
			
			BizAttributeType[] bizAttributeTypes = watchInvokerType.getBizAttributeArray();
			if (bizAttributeTypes != null){
				for (int i=0;i < bizAttributeTypes.length;i++){
					DataRow row = new DataRow();
					BizAttributeType bizAttributeType = bizAttributeTypes[i];
					String code = param.get("attrCode_"+i,bizAttributeType.getCode());
					String value = param.get("attrValue_"+i,bizAttributeType.getValue());
					row.put("code",code);
					row.put("value",value);
					attributes.add(row);
				}
			}
		}
		this.setAttribute("attributeSize", attributes.size());
	}
	
	@PageAction
	public ViewRenderer deleteBizAttribute(DataParam param){
		int attributeIndex = param.getInt("attributeIndex");
		
		String activityId = param.get("WFA_ID");
		String interceptorId = param.get("interceptorId");
		String positionType = param.get("positionType");
		ProcessDefine processDefine = getProcessDefine();
		if (!StringUtil.isNullOrEmpty(interceptorId)){
			InterceptorModel interceptorModel = ProcessDefineUtil.getInterceptorModel(processDefine
					,positionType, activityId, interceptorId);
			WatchInvokerType watchInvokerType = interceptorModel.getWatchInvoker();
			BizAttributeType[] bizAttributeTypes = watchInvokerType.getBizAttributeArray();
			if (bizAttributeTypes != null){
				watchInvokerType.removeBizAttribute(attributeIndex);
			}
		}
		return prepareDisplay(param);
	}
	
	public ViewRenderer doShowVariableBoxAction(DataParam param){
		ProcessDefine processDefine = getProcessDefine();
//		String activityId = param.get("WFA_ID");
		VariableBox variableSelectBox = new VariableBox(processDefine,param);
		return variableSelectBox.doShowVariableBoxAction();
	}
	public ViewRenderer doChangeVariableTypeAction(DataParam param){
		ProcessDefine processDefine = getProcessDefine();
//		String activityId = param.get("WFA_ID");
		VariableBox variableSelectBox = new VariableBox(processDefine,param);
		return variableSelectBox.doChangeVariableTypeAction();
	}
	public ViewRenderer doSelectVariableAction(DataParam param){
		ProcessDefine processDefine = getProcessDefine();
//		String activityId = param.get("WFA_ID");
		VariableBox variableSelectBox = new VariableBox(processDefine,param);
		return variableSelectBox.doSelectVariableAction();
	}
}
